from aioredis import Redis
from fastapi import Depends
from peewee_async import Manager

from common.utils.depends.client_depends import get_cli_tenant_id, get_cli_user_id
from common.utils.depends.common import get_mgw, get_redis, get_tenant_id, get_current_user_id, get_request_id, \
    get_mall_id
from common.utils.resp import JsonResponse, RC


class BaseView:
    mgw: Manager = Depends(get_mgw)
    redis: Redis = Depends(get_redis)
    request_id: str = Depends(get_request_id)

    def unpack_response(self, ok, data_or_code):
        if ok:
            return JsonResponse(data=data_or_code.data).to_response()
        else:
            return JsonResponse(code=data_or_code.code, msg=data_or_code.msg).to_response()

    def unpack_model_response(self, ok, data_or_code, response_model):
        if ok:
            return response_model(code=RC.SUCCESS, data=data_or_code.data)
        else:
            return response_model(code=data_or_code.code, msg=data_or_code.msg)


class AdminBaseView(BaseView):
    ...


class TenantBaseView(BaseView):
    tenant_id: int = Depends(get_tenant_id)
    user_id: int = Depends(get_current_user_id)
    mall_id: int = Depends(get_mall_id)

class ClientBaseView(BaseView):
    tenant_id: int = Depends(get_cli_tenant_id)
    mall_id: int = Depends(get_mall_id)
    user_id: int = Depends(get_cli_user_id)


